﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using l1cs.server.templates;
using MySql.Data.MySqlClient;
using l1cs.server.utils;
using System.Collections;

namespace l1cs.server.datatables
{
    public class AuctionBoardTable
    {
        private static Logger _log = Logger.getLogger( typeof( AuctionBoardTable ).FullName );

        private static AuctionBoardTable _instance;

        /// <summary>スレッドセーフなハッシュ(int, L1AuctionBoard)</summary>
        private Hashtable _boards = Hashtable.Synchronized( new Hashtable() );

        public static AuctionBoardTable getInstance()
        {
            if ( _instance == null )
            {
                _instance = new AuctionBoardTable();
            }
            return _instance;
        }

        /// <summary>
        /// 競売掲示板のコンストラクター
        /// </summary>
        public AuctionBoardTable()
        {/*TTOK*/
            String sqlstr = "SELECT * FROM board_auction ORDER BY house_id";
            DataTable dt;
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                dt = wrapper.GetDataTable( sqlstr );
            }
            foreach ( DataRow item in dt.Rows )
            {
                L1AuctionBoard board = new L1AuctionBoard();
                board.setHouseId( SqlUtils.toInt( item["house_id"] ) );
                board.setHouseName( Convert.ToString( item["house_name"] ) );
                board.setHouseArea( SqlUtils.toInt( item["house_area"] ) );
                board.setDeadline( SqlUtils.toDateTime( item["deadline"] ) );
                board.setPrice( SqlUtils.toInt( item["price"] ) );
                board.setLocation( Convert.ToString( item["location"] ) );
                board.setOldOwner( Convert.ToString( item["old_owner"] ) );
                board.setOldOwnerId( SqlUtils.toInt( item["old_owner_id"] ) );
                board.setBidder( Convert.ToString( item["bidder"] ) );
                board.setBidderId( SqlUtils.toInt( item["bidder_id"] ) );
                _boards[board.getHouseId()] = board;
            }
        }

        /// <summary>
        /// 競売掲示板の全リストを返す(あまりよろしくないような。。。）
        /// </summary>
        /// <returns></returns>
        public L1AuctionBoard[] getAuctionBoardTableList()
        {
            // TODO 優先度8 Hashtableのコピー渡しよりいい方法はないか？
            L1AuctionBoard[] ret = new L1AuctionBoard[_boards.Count];
            _boards.Values.CopyTo( ret, 0 );
            return ret;
        }

        /// <summary>
        /// アジトIDから競売掲示板を返す。
        /// </summary>
        /// <param name="houseId">アジトID</param>
        /// <returns></returns>
        public L1AuctionBoard getAuctionBoardTable( int houseId )
        {/*TTOK*/
            L1AuctionBoard ret = (L1AuctionBoard)_boards[houseId];
            return ret;
        }

        /// <summary>
        /// DBに競売情報を追加する。
        /// </summary>
        /// <param name="board">競売情報</param>
        public void insertAuctionBoard( L1AuctionBoard board )
        {/*TTOK*/
            String sqlstr = "INSERT INTO board_auction SET " +
                            "house_id     =?1, " +
                            "house_name   =?2, " +
                            "house_area   =?3, " +
                            "deadline     =?4, " +
                            "price        =?5, " +
                            "location     =?6, " +
                            "old_owner    =?7, " +
                            "old_owner_id =?8, " +
                            "bidder       =?9, " +
                            "bidder_id    =?10";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int24 ).Value = board.getHouseId();
                cmd.Parameters.Add( "?2", MySqlDbType.String ).Value = board.getHouseName();
                cmd.Parameters.Add( "?3", MySqlDbType.Int24 ).Value = board.getHouseArea();
                cmd.Parameters.Add( "?4", MySqlDbType.DateTime ).Value = board.getDeadline();
                cmd.Parameters.Add( "?5", MySqlDbType.Int24 ).Value = board.getPrice();
                cmd.Parameters.Add( "?6", MySqlDbType.String ).Value = board.getLocation();
                cmd.Parameters.Add( "?7", MySqlDbType.String ).Value = board.getOldOwner();
                cmd.Parameters.Add( "?8", MySqlDbType.Int24 ).Value = board.getOldOwnerId();
                cmd.Parameters.Add( "?9", MySqlDbType.String ).Value = board.getBidder();
                cmd.Parameters.Add( "?10", MySqlDbType.Int24 ).Value = board.getBidderId();
                wrapper.ExecNonQuery( cmd );
            }

            _boards[board.getHouseId()] = board;
        }

        /// <summary>
        /// 競売情報を更新する。
        /// </summary>
        /// <param name="board"></param>
        public void updateAuctionBoard( L1AuctionBoard board )
        {/*TTOK*/
            String sqlstr = "UPDATE board_auction SET " +
                            "house_name   =?1, " +
                            "house_area   =?2, " +
                            "deadline     =?3, " +
                            "price        =?4, " +
                            "location     =?5, " +
                            "old_owner    =?6, " +
                            "old_owner_id =?7, " +
                            "bidder       =?8, " +
                            "bidder_id    =?9 " +
                            "WHERE house_id=?10";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.String ).Value = board.getHouseName();
                cmd.Parameters.Add( "?2", MySqlDbType.Int24 ).Value = board.getHouseArea();
                cmd.Parameters.Add( "?3", MySqlDbType.DateTime ).Value = board.getDeadline();
                cmd.Parameters.Add( "?4", MySqlDbType.Int24 ).Value = board.getPrice();
                cmd.Parameters.Add( "?5", MySqlDbType.String ).Value = board.getLocation();
                cmd.Parameters.Add( "?6", MySqlDbType.String ).Value = board.getOldOwner();
                cmd.Parameters.Add( "?7", MySqlDbType.Int24 ).Value = board.getOldOwnerId();
                cmd.Parameters.Add( "?8", MySqlDbType.String ).Value = board.getBidder();
                cmd.Parameters.Add( "?9", MySqlDbType.Int24 ).Value = board.getBidderId();
                cmd.Parameters.Add( "?10", MySqlDbType.Int24 ).Value = board.getHouseId();
                wrapper.ExecNonQuery( cmd );
            }
        }

        /// <summary>
        /// 競売情報を削除する。
        /// </summary>
        /// <param name="houseId"></param>
        public void deleteAuctionBoard( int houseId )
        {/*TTOK*/
            String sqlstr = "DELETE FROM board_auction WHERE house_id = ?1";
            using ( MySqlWrapper wrapper = new MySqlWrapper() )
            {
                MySqlCommand cmd = new MySqlCommand( sqlstr );
                cmd.Parameters.Add( "?1", MySqlDbType.Int24 ).Value = houseId;
                wrapper.ExecNonQuery( cmd );
            }
        }
    }
}
